package com.ezhuan.system.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ezhuan.common.dao.impl.BaseDaoImpl;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.dao.IACateDao;
import com.ezhuan.system.pojo.ACate;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ACateDaoImpl extends BaseDaoImpl<ACate> implements IACateDao
{
	private ACate cate = new ACate();
	
	@Override
	public ACate findOneDao(Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ cate.getTableName() +" where 1 = 1 ");
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 按照ID查询 */
			sqlSb.append(" and id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 木有任何条件,直接返回Null */
			return null ; 
		}
		Record record = Db.findFirst(sqlSb.toString(), paramsList.toArray());
		if (record != null)
		{
			/* 用户信息 */
			ACate cate = new ACate();
			return cate.parseRecord(record);
		}
		return null;
	}

	@Override
	public List<ACate> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ cate.getTableName() +" where 1 = 1 ");
		
		/* 按照状态查询 */
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sqlSb.append(" and status = ? ");
			paramsList.add(condMap.get("status"));
		}
		
		/* 按照状态查询 */
		if (condMap.get("cateType") != null && !"".equalsIgnoreCase(condMap.get("cateType") + ""))
		{
			sqlSb.append(" and cateType = ? ");
			paramsList.add(condMap.get("cateType"));
		}
		
		/* 排序 */
		if ("pubTimeDesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			/* 今日收益排序 */
			sqlSb.append(" order by pubTime desc  ");
		}else
		{
			/* 默认排序 */
			sqlSb.append(" order by pubTime desc  ");
		}
		
		List<Record> recordList = Collections.EMPTY_LIST ; 
		/* 分页 */
		if (pageInfoUtil != null)
		{
			recordList = this.findByPage(pageInfoUtil, sqlSb.toString(), paramsList);
		}else
		{
			recordList = this.find(sqlSb.toString(), paramsList);
		}

		/* 存储结果 */
		List<ACate> channelList = new ArrayList<ACate>();
		for (Iterator iterator = recordList.iterator(); iterator.hasNext();)
		{
			Record record = (Record) iterator.next();
			/* 用户信息 */
			ACate channel = new ACate();
			channel = channel.parseRecord(record);
			channelList.add(channel);
		}
		return channelList;
	}
}
